package logic

import (
	"context"
	"encoding/json"
	"furni-suite/cache"
	"furni-suite/model"
	"time"

	"furni-suite/furnirpc/furnirpc"
	"furni-suite/furnirpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserShowLogic {
	return &UserShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 用户列表
func (l *UserShowLogic) UserShow(in *furnirpc.UserShowRequest) (*furnirpc.UserShowResponse, error) {
	// todo: add your logic here and delete this line
	var user model.User
	result, _ := cache.Rdb.Get(cache.Ctx, "userList").Result()
	if result == "" {
		user.UserShow(model.DB, int(in.UserId))
		marshal, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, "userList", marshal, time.Hour*1)
	} else {
		json.Unmarshal([]byte(result), &user)
	}
	return &furnirpc.UserShowResponse{
		Username: user.Username,
		Password: user.Password,
	}, nil
}
